//
//  BRStatilListTableViewCell.m
//  BrezzeSaaS
//
//  Created by IOS on 2021/9/24.
//  Copyright © 2021 Brezze GWG. All rights reserved.
//

#import "BRStatilListTableViewCell.h"

@implementation BRStatilListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.backgroundColor  = UIColor.clearColor;
    self.contentView.backgroundColor  = UIColor.clearColor;
    self.contentBGView.backgroundColor = UIColor.whiteColor;
    self.nameL.font = [UIFont fontB:17];
    self.nameL.textColor = kColorTitle;
    self.subL.font = [UIFont fontR:12];
    self.subL.textColor = kColorTitle888;
    self.equipmentCountL.font = [UIFont fontR:11];
    self.equipmentCountL.textColor = kColorMain;
    self.emptyLB.font = [UIFont fontR:11];
    self.emptyLB.textColor = kColorMain;
    self.equipmentCountL.text = [NSString stringWithFormat:@"%@:",Localized(@"home_rent_tag")];
    self.emptyLB.text = [NSString stringWithFormat:@"%@:",Localized(@"home_return_tag")];
    self.avNumL.font = [UIFont fontB:17];
    self.avNumL.textColor = kColorMain;
    self.emptyNumL.font = [UIFont fontB:17];
    self.emptyNumL.textColor = kColorMain;
    
    self.locL.font = [UIFont br_fontM:10];
    self.locL.textColor = kColorMain;
    self.locaBgV.backgroundColor = kUIColorMainTint;
//    self.locaBgV.layer.borderColor = kUIColorMainTint.CGColor;
//    self.locaBgV.layer.borderWidth = 0.5;
    self.locaBgV.layer.cornerRadius = 14;
    self.locaBgV.layer.masksToBounds = YES;
    self.stationImg.image = UIImage.qd_stationDefaultImg;
    self.imgShadowView.backgroundColor = UIColorMakeWithRGBA(0, 0, 0, 0.4);
    UITapGestureRecognizer *tap  = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(locationAction)];
    [self.locImg addGestureRecognizer:tap];
    self.locImg.userInteractionEnabled = YES;
}

- (void)locationAction {
    
    NSLog(@"123");
    if (self.locationClick) {
        self.locationClick();
    }
}

- (void)setIsOn:(NSInteger)isOn {
    _isOn = isOn;
    BOOL hidden = isOn == 1 ? NO : YES;
    self.imgShadowView.hidden = !hidden;
//    self.emptyLB.hidden = hidden;
//    self.locImg.image = isOn == 1 ?  UIImageMake(@"station_locationTo") : UIImageMake(@"station_locationUnto");
    if (isOn == 1) {
        self.nameL.textColor = kColorTitle;
        self.equipmentCountL.textColor = kColorMain;
        self.equipmentCountL.text = [NSString stringWithFormat:@"%@:",Localized(@"home_rent_tag")];
        self.emptyLB.textColor = kColorMain;
        return;
    }
    self.nameL.textColor = kColorTitle888;
    self.equipmentCountL.textColor = kColorTitle888;
    self.equipmentCountL.text = Localized(@"home_outOfService");
    self.emptyLB.textColor = kColorTitle888;
}

- (void)setDto:(BRStationListModel *)dto {
    _dto = dto;
    
    // 使用新接口字段（兼容方法会自动处理）
    self.nameL.text = dto.branchName;  // 会优先使用 name
    self.subL.text = dto.address ?: @"";
    
    // 距离显示 - 新接口V2的distance是公里（浮点数），直接使用公里显示
    NSString *locStr;
    if (dto.distance < 1.0) {
        // 小于1公里，显示小数点后2位
        locStr = [NSString stringWithFormat:@"%.2f km", dto.distance];
    } else if (dto.distance < 10.0) {
        // 1-10公里，显示小数点后1位
        locStr = [NSString stringWithFormat:@"%.1f km", dto.distance];
    } else {
        // 大于10公里，显示整数
        locStr = [NSString stringWithFormat:@"%.0f km", dto.distance];
    }
    self.locL.text = locStr;
    
    // 使用新接口的字段
    self.emptyNumL.text = [NSString stringWithFormat:@"%ld", (long)dto.depotEmpty];
    
    // 营业状态 - 使用新接口的open
    self.isOn = dto.open ? 1 : 0;
    if (self.isOn) {
        self.avNumL.text = [NSString stringWithFormat:@"%ld", (long)dto.depotBorrow];
        self.emptyLB.text = [NSString stringWithFormat:@"%@:",Localized(@"home_return_tag")];
    }else {
        self.avNumL.text = @"";
        self.emptyLB.text = locStr;
    }
    self.avNumL.hidden = !self.isOn;
    self.emptyNumL.hidden = !self.isOn;
    
    NSString *fileS = USERManager.filesSever;
    NSString *imgUrl = [NSString stringWithFormat:@"%@%@",fileS ?: @"", dto.logo ?: @""];
    [self.stationImg sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:UIImage.qd_stationDefaultImg];
}

- (NSAttributedString *)openAttString:(NSString *)openTime {
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:openTime];
    if ([openTime containsString:@"Open"]) {
        //如果包含了open
        NSRange range = [BRTools rangeof:@"Open" toStr:openTime];
        
        [attStr addAttribute:NSForegroundColorAttributeName value:UIColorGreen range:range];
        return attStr;
        
    }else {
        if ([openTime containsString:@"Closed"]) {
            NSRange range = [BRTools rangeof:@"Closed" toStr:openTime];
            
            [attStr addAttribute:NSForegroundColorAttributeName value:UIColorRed range:range];
            return attStr;
        }
    }
    
    return attStr;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
